package com.qms.repast.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qms.repast.App;
import com.qms.repast.R;
import com.qms.repast.commons.Constants;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.RecycleViewItemData;
import com.qms.repast.entity.reqbean.SearchBean;
import com.qms.repast.ui.adapter.ReserveAdapter;
import com.qms.repast.ui.base.BaseFragment;
import com.qms.repast.ui.presenter.RecycleViewPresenter;
import com.qms.repast.ui.view.IRecycleView;
import com.qms.repast.utils.AppUtils;
import com.qms.repast.utils.SpUtils;
import com.qms.repast.weidgets.MyDecoration;
import com.qms.repast.weidgets.adsorptionhelper.HeaderScrollHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.qms.repast.entity.resbean.ShopInfo.BodyBean.RowsBean;

/**
 * @author 泅渡者
 * @date 2018/9/20 0020
 * @describe:
 */
public class RecyclerViewFragment extends BaseFragment<RecycleViewPresenter> implements IRecycleView, HeaderScrollHelper.ScrollableContainer, OnLoadMoreListener, OnRefreshListener {
	@BindView(R.id.recyclerview)
	RecyclerView recyclerview;
	@BindView(R.id.refreshLayout)
	SmartRefreshLayout refreshLayout;

	private List<RecycleViewItemData> dataList;
	private ReserveAdapter reserveAdapter;
	private LinearLayoutManager linearLayoutManager;
	private GridLayoutManager gridLayoutManager;
	/**
	 * 当前页号，默认第1页
	 */
	private int pageNumber = 1;
	/**
	 * 每页显示多少条，默认50条
	 */
	private static String pageSize = "50";

	@Override
	public View getScrollableView () {
		return recyclerview;
	}

	@Override
	protected int initLayout () {
		return R.layout.fragment_recycleview;
	}

	@Override
	protected void initPresenter () {
		mPresenter = new RecycleViewPresenter(getActivity(), this);
	}

	@Subscribe
	public void onEventMainThread (MessageEvent messageEvent) {
		switch (messageEvent.getCode()) {
			//改变为网格
			case 2011:
				recyclerview.getRecycledViewPool().clear();
				recyclerview.setLayoutManager(gridLayoutManager);
				break;
			//改变为列表
			case 2012:
				recyclerview.getRecycledViewPool().clear();
				recyclerview.setLayoutManager(linearLayoutManager);
				break;
			case 1012:
				pullData();
				break;
			default:
				break;
		}
	}

	@Override
	protected void pullData () {
		SearchBean searchBean = new SearchBean();
//		searchBean.setCityID((String) SpUtils.getParam(AppUtils.getContext(), Constants.CITYID, ""));
		searchBean.setCityID("610100");
		searchBean.setLng((String) SpUtils.getParam(AppUtils.getContext(), Constants.LNG, ""));
		searchBean.setLat((String) SpUtils.getParam(AppUtils.getContext(), Constants.LAT, ""));
		searchBean.setOffset(pageSize);
		searchBean.setCurrent(pageNumber + "");
		searchBean.setKeyword("");
		searchBean.setSort("");
		mPresenter.getShopList(searchBean);
	}

	@Override
	protected void initView (View view, Bundle savedInstanceState) {

		EventBus.getDefault().register(this);

		dataList = new ArrayList<>();
		reserveAdapter = new ReserveAdapter(App.getContext());
		linearLayoutManager = new LinearLayoutManager(getHoldingActivity());
		gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);

		recyclerview.setAdapter(reserveAdapter);
		recyclerview.setLayoutManager(linearLayoutManager);
		recyclerview.addItemDecoration(new MyDecoration(getHoldingActivity(), MyDecoration.VERTICAL_LIST, R.drawable.draw_line));

		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setOnLoadMoreListener(this);

		refreshLayout.setEnableRefresh(true);
		refreshLayout.setEnableLoadMore(true);

	}

	@Override
	public void onDestroy () {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
	}

	@Override
	public void onLoadMore (@NonNull RefreshLayout refreshLayout) {
		pageNumber++;
		pullData();
	}

	@Override
	public void initShopList (List<RowsBean> shopsBeans) {

		for (RowsBean rowsBean : shopsBeans) {
			dataList.add(new RecycleViewItemData(rowsBean, RecycleViewItemData.TYPE_NORMAL_LAYOUT));
		}
		reserveAdapter.addList(dataList);

		refreshLayout.finishLoadMore();
		refreshLayout.finishRefresh();
	}

	@Override
	public void onRefresh (@NonNull RefreshLayout refreshLayout) {
		pageNumber = 1;
		pullData();
	}
}
